package com.tttn.docbao.db;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.UnknownHostException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.util.Log;

/*
 * Classname     :ConnectToNetwork
 *
 * Version info  :1.0
 * 
 * Description   :ConnectToNetwork deals with the connnection with server and its response
 */
public class ConnectToNetwork {

	public static String getData(String url) {

		InputStream instream = null;
		HttpParams params = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(params, 10000);
		HttpConnectionParams.setSoTimeout(params, 12000);
		HttpClient httpclient = new DefaultHttpClient(params);
		String url1 = (url);

		HttpResponse response;
		StringBuilder sb = null;
		try {
			HttpGet httpget = new HttpGet(url1);

			sb = new StringBuilder();
			response = httpclient.execute(httpget);
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					instream = entity.getContent();
				}
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(instream));
				String line = null;
				try {
					while ((line = reader.readLine()) != null) {
						sb.append(line + "\n");
					}
				} catch (IOException e) {

				} catch (Exception r) {

				} finally {
					try {
						instream.close();
					} catch (IOException e) {

						e.printStackTrace();
					}
				}
			}
		} catch (ConnectTimeoutException e) {
			return "timeout";

		} catch (UnknownHostException e) {

			return "no network";
		} catch (IOException e) {

			return "no network";
		} catch (Exception e) {
			Log.e("error thiendv", e.toString());
			return "exception";
		}

		return sb.toString();
	}
}